package com.geeklan.smart.ui.message.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.geeklan.smart.data.model.Message;
import com.geeklan.smart.data.model.PetInfo;
import com.geeklan.smart.databinding.LayoutMessageListItemBinding;
import com.geeklan.smart.databinding.LayoutPetListItemBinding;
import com.geeklan.smart.widget.ItemOnClickListener;

import java.util.List;

public class MessageListAdapter extends RecyclerView.Adapter<MessageListAdapter.ItemViewHolder> {
    private Context mContext;
    private List<Message> itemList;
    private ItemOnClickListener itemOnClickListener;

    public void setItemOnClickListener(ItemOnClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }

    public MessageListAdapter(Context context, List<Message> itemList) {
        this.itemList = itemList;
        this.mContext = context;
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MessageListAdapter.ItemViewHolder(LayoutMessageListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        Message message = itemList.get(position);
        holder.mBinding.view.setVisibility(message.getReadFlag() == 0 ? View.VISIBLE:View.INVISIBLE);
        holder.mBinding.tvMessageTitle.setText(message.getTitle());
        holder.mBinding.tvMessage.setText(message.getContent());
        holder.mBinding.tvDate.setText(message.getCreateTime());
        holder.mBinding.tvRead.setVisibility(message.getReadFlag() == 0 ? View.VISIBLE:View.INVISIBLE);
        holder.mBinding.tvRead.setOnClickListener(v -> {
            itemOnClickListener.read(position);
        });
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        LayoutMessageListItemBinding mBinding;

        public ItemViewHolder(@NonNull LayoutMessageListItemBinding itemView) {
            super(itemView.getRoot());
            mBinding = itemView;
        }
    }

    public interface ItemOnClickListener {
        void read(int position);
    }
}